<?php
/**
 * 请假相关功能接口控制器
 * 
 */
class leaveAction extends apiAction {
	/**
	 * 我的请假历史
	 */
	public function index(){
		$page	= I('page', 1);	// 要显示第几页数据
		$num	= I('num', 20);	// 每页显示多少条数据
		
		$data = array();
		$leaveType = C('LEAVE_TYPE'); //请假类型
		$verifyType = C('LEAVE_VERIFY_TYPE'); //审批状态类型
		$list = D('leave')->where(array('userId'=> $this->user['id']))->limit($num)->page($page)->order('id DESC')->select();
		
		foreach ($list as $n){
			$temp = array();
			
			$temp['type']		= $leaveType[$n['type']];	//请假类型
			$temp['createTime']	= date('Y-m-d H:i', $n['createTime']);	//发起时间
			$temp['startTime']	= date('Y-m-d', $n['startTime']);	//假期开始时间
			$temp['endTime']	= date('Y-m-d', $n['endTime']);	//假期结束时间
			$temp['days']		= abs((($n['endTime'] - $n['startTime']) / (3600 * 24))) + 1;	//请假天数
			$temp['verifyType']	= $n['verifyType'] ? $verifyType[$n['verifyType']] : "未处理";	//审核状态
			
			$data[] = $temp;
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 获得请假类型
	 */
	public function leaveType(){
		$data = array();
		$leaveType = C('LEAVE_TYPE');			//请假类型
		foreach ($leaveType as $key=>$val){
			$data[] = array('typeId'=>$key, 'typeName'=>$val);
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 请假申请提交接口
	 */
	public function apply(){
		$data['userId']		= $this->user['id'];
		$data['type']		= I('type');
		$data['remark']		= I('remark');
		$data['startTime']	= I('startTime');
		$data['endTime']	= I('endTime');
		$data['createTime']	= time();
		
		$module = D(MODULE_NAME);
		if (!$module->create($data)){
			$this->ajaxReturn(300, $module->getError());
		}
		
		$data['startTime'] = strtotime($data['startTime']);
		$data['endTime'] = strtotime($data['endTime']);
		
		if($data['endTime'] < $data['startTime']){
			$this->ajaxReturn(300, L('date_is_error'));
		}
		
		if($module->add($data)){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}
	
	/**
	 * 请假审核列表
	 */
	public function verifyList(){
		$ids = D('admin')->get_subordinate_ids();
        if(!empty($ids)){
		  $data= D('leave')->getVerifyList($ids);

          $this->ajaxReturn(200, L('success'), $data);
        }else{
          $this->ajaxReturn(300, '找不到下属');  
        }
		
	}
	
	
	/**
	 * 获得请假审批详情
	 */
	public function verifyDetail(){
		$leaveId = I('leaveId'); //请假记录id
		if(! $leaveId){
			$this->ajaxReturn(300, L('leave_id_is_require'));
		}
		$data	= D('leave')->getLeaveDetail($leaveId);
		
		//如果为未审核，则显示审核选项
		if(! $data['isVerify']){
			$data['verifyOptions'] =  D('leave')->getVerifyOptions();
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 假期审核结果提交
	 */
	public function verifySubmit(){
		$leaveId = I('leaveId');
		
		$data['verifyType'] = I('verifyTypeId');	//审核结果类型
		$data['verifyRemark'] = I('verifyRemark');	//审核结果备注
		$data['verifyTime'] = time();
		
		if( $leaveId && D('leave')->where(array('id'=>$leaveId))->save($data) ){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}
	
}

/* End of file leaveAction.class.php */